﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace VirtualDisk
{
    class CompareCommand : Command
    {
        private MyString[] arguments = null;

        protected override int run()
        {
            if (arguments == null || arguments.Length != 2)
            {
                return 1;
            }

            MyString lhs = arguments[0].Trim();
            MyString rhs = arguments[1].Trim();
            if (!File.Exists(lhs.ToString()))
            {
                return 2;
            }

            if (!DiskTree.getInstance().isExist(rhs) || DiskTree.getInstance().isADirctory(rhs))
            {
                return 3;
            }

            //扩展名是否相同
            if (DiskHelper.getExtension(rhs).ToString() != Path.GetExtension(lhs.ToString()))
            {
                return 4;
            }

            MyString extension = DiskHelper.getExtension(rhs);
            Byte[] dataOfRhs = DiskTree.getInstance().readFileData(rhs);
            using (FileStream fs = new FileStream(lhs.ToString(), FileMode.Open))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    Byte[] dataOfLhs = new Byte[fs.Length];
                    br.Read(dataOfLhs, 0, dataOfLhs.Length);
                    //获得不同处
                    int diffPoint = compareByte(dataOfLhs, dataOfRhs);
                    if (diffPoint == -1)
                    {
                        Console.WriteLine("内容比较一致！");
                    }
                    else
                    {
                        //打印不同
                        Console.WriteLine("不同处: "+(diffPoint + 1).ToString());
                        Console.WriteLine("第一个文件：");
                        printDiff(diffPoint, dataOfLhs, extension);
                        Console.WriteLine();
                        Console.WriteLine("第二个文件：");
                        printDiff(diffPoint, dataOfRhs, extension);
                        Console.WriteLine();
                    }
                }
            }
            return 0;
        }

        protected override void processError(int returnCode)
        {
            switch (returnCode)
            {
                case 1:
                    Console.WriteLine("命令语法不正确!");
                    break;
                case 2:
                    Console.WriteLine("系统找不到指定的文件！" + arguments[0].ToString());
                    break;
                case 3:
                    Console.WriteLine("系统找不到指定的文件！" + arguments[1].ToString());
                    break;
                case 4:
                    Console.WriteLine("文件类型不同，无法比较！");
                    break;
                default:
                    break;
            }
        }

        private void printDiff(int diff, Byte[] byte1, MyString extension)
        {

            int len = (byte1.Length - diff > 16) ? 16 + diff : byte1.Length;
            for (int i = diff; i < len; i++)
            {
                if (extension == new MyString(".txt"))
                {
                    Console.Write(Convert.ToChar(byte1[i]));
                }
                else
                {
                    Console.Write(byte1[i].ToString("X"));
                }

            }



        }
        //返回两个字节数组第一个不同处，如果相同返回-1
        private int compareByte(Byte[] byte1, Byte[] byte2)
        {
            int lenLhs = byte1.Length;
            int lenRhs = byte2.Length;
            int diffrentPoint = -1;
            int looplen = lenLhs > lenRhs ? lenRhs : lenLhs;
            for (int i = 0; i < looplen; i++)
            {
                if (byte1[i] != byte2[i])
                {
                    diffrentPoint = i;
                    break;
                }
            }
            //数组大小不同时，不同处即为小数组的length处
            if (diffrentPoint == -1 && lenLhs != lenRhs)
            {
                diffrentPoint = (lenLhs > lenRhs) ? lenRhs : lenLhs;
            }
            return diffrentPoint;

        }


        public CompareCommand(MyString[] argu)
        {
            if (argu != null)
            {
                arguments = new MyString[argu.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    arguments[i] = argu[i];
                }
            }
        }
    }
}
